package org.rcy.framework.mq.serializer;

import com.rabbitmq.client.AMQP;
import org.rcy.framework.api.mq.AbstractMessage;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/11/11 17:53
 */


public class MessageSerializer {
	public MessageSerializer() {
	}

	public static byte[] serialize(AbstractMessage message) throws Exception {
		return getSerializer().serialize(message);
	}
	public static <M extends AbstractMessage> M deserialize(byte[] body, AMQP.BasicProperties properties) throws Exception {
		String type = properties.getType();
		Class<?> clazz = Class.forName(type);
		return (M) getSerializer().deserialize(body, clazz);
	}

	private static <M extends AbstractMessage> Serializer<M> getSerializer(){
		return JsonSerializer.getInstance();
	}
}
